package com.example.demo.service.impl;
import com.google.common.collect.Maps;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.auth.OssSignatureComposer;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

import com.example.demo.service.SMSMsgService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author lyc
 * @date 2019/6/10.
 */
@Service
@Slf4j
public class SMSMsgServiceImpl implements SMSMsgService {

    private String smsT = "验证码为：${code}，您正在登录，若非本人操作，请勿泄露";

    @Override
    public String send(String tel) {
       DefaultProfile profile = DefaultProfile.getProfile("default", "LTAIEu7GG9NQnUo7", "dyZLqocz64r26E4dblQY69upQjaFTq");
        IAcsClient client = new DefaultAcsClient(profile);
        String code = RandomStringUtils.randomNumeric(4);
        CommonRequest request = new CommonRequest();
        //request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("SignName", "礼单小助手");
        request.putQueryParameter("PhoneNumbers", tel);
        request.putQueryParameter("TemplateCode", "SMS_166375881");
        request.putQueryParameter("TemplateParam","{'code':"+code+"}" );
        request.setHttpContent(smsT.getBytes(),"utf-8", FormatType.JSON);
        try {
            CommonResponse response = client.getCommonResponse(request);
           log.info("短信响应：{}",response.getData());
        } catch (ClientException e) {
           log.error("短信错误：{}",e);
        }

        return code;
    }
}